package com.bdqn.crm.controller;

import com.alibaba.druid.sql.visitor.functions.Right;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.crm.pojo.PageBean;
import com.bdqn.crm.pojo.Pagee;
import com.bdqn.crm.pojo.SysRight;
import com.bdqn.crm.pojo.SysRole;
import com.bdqn.crm.service.SysRightService;
import com.bdqn.crm.service.SysRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysRightService sysRightService;

    @RequestMapping(value = "role/list")
    public String list(Model model, String roleName, Long pageIndex) {
        //数据回显
        model.addAttribute("roleName", roleName);
        //如果为空，查询第一页
        if (pageIndex == null) {
            pageIndex = 1L;
        }

        Page<SysRole> page = new Page<SysRole>();
        page.setCurrent(pageIndex);
        IPage<SysRole> pageBean = sysRoleService.page(roleName, page);
        model.addAttribute("rolePage", pageBean);
//        Pagee<SysRole> pageBean = sysRoleService.page(roleName,pageIndex);
//        model.addAttribute("rolePage",pageBean);
        return "role/list";
    }

    /**
     * 跳转添加页面(查询权限)
     *
     * @param model
     * @return
     */
    @GetMapping(value = "role/add")
    public String add(Model model) {
        //查询所有权限,用于指派权限
        List<SysRight> rights = sysRightService.findAllRights();
        model.addAttribute("rights", rights);
        return "role/add";
    }

    /**
     * 编辑页面
     *
     * @param model
     * @param roleId
     * @return
     */
    @GetMapping(value = "role/edit")
    public String edit(Model model, Long roleId) {
        SysRole sysRole = sysRoleService.selectRoleId(roleId);
        model.addAttribute("role", sysRole);

        //查找角色拥有的权限
        List<SysRight> roleRight = sysRightService.findRightsByRole(sysRole);
        model.addAttribute("roleRights", roleRight);

        //查询所有权限,用于指派权限
        List<SysRight> rights = sysRightService.findAllRights();
        model.addAttribute("rights", rights);
        return "role/edit";
    }

    @PostMapping(value = "role/save")
    public String editSave(SysRole role, String[] rightCodes) {
        if (rightCodes != null) {
            for (String rightCode : rightCodes) {
                SysRight right = new SysRight();
                right.setRightCode(rightCode);
                role.getRights().add(right);//将关联的权限添加到角色中，用于级联增关联表的数据
            }
        }
        boolean flgs = sysRoleService.saveRole(role);  //新增或修改
        if (flgs == true) {
            return "redirect:/role/list";
        }
        return "role/edit";
    }
    @ResponseBody
    @GetMapping("role/del/{roleId}")
    public Map del(Long roleId) {
        sysRoleService.removeById(roleId);
        Map map = new HashMap();
        map.put("delResult", "true");
        return map;
    }
}
